#include"SingleList.h"


void SLPrint(SLNode* phead)
{
	SLNode* cur = phead;
	while (cur != NULL)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}

SLNode* BuySLNode(SLDataType x)
{
	SLNode* newnode = (SLNode*)malloc(sizeof(SLNode));
	if (newnode == NULL)
	{
		perror("BuySLNode->malloc failed");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}

void SLPushBack(SLNode** pphead, SLDataType x)
{
	SLNode* newnode = BuySLNode(x);
	if (*pphead == NULL)
	{
		*pphead = newnode;
	}
	else
	{
		SLNode* tail = *pphead;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
}


void SLPushFront(SLNode** pphead, SLDataType x)
{
	SLNode* newnode = BuySLNode(x);
	newnode->next = *pphead;
	*pphead = newnode;
}


void SLPopBack(SLNode** pphead)
{
	assert(*pphead);
	SLNode* tmp = NULL;
	SLNode* tail = *pphead;
	while (tail->next != NULL)
	{
		tmp = tail;
		tail = tail->next;
	}
	if (tmp != NULL)
	{
		free(tail);
		tmp->next = NULL;
	}
	else
	{
		free(*pphead);
		*pphead = NULL;
	}
}

void SLPopFront(SLNode** pphead)
{
	assert(*pphead);
	SLNode* newhead = (*pphead)->next;
	free(*pphead);
	*pphead = newhead;
}


